<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Article;
use yii;


class ArticleLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getArticle($article_type,$query = [], $page = 1, $limit = 10)
    {
        $article = Article::find()->filterWhere($query)->andWhere("article_type=$article_type")->andWhere("is_del=0")
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $article->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $article->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'article/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getById($id)
    {
        return Article::find()->where(['article_id' => $id])->asArray()->one();
    }


    public function saveStatus($id)
    {
        $article = Article::findOne($id);
        if($article->status)
            $article->status = 0;
        else
            $article->status = 1;
        return $article->save();
    }

    public function addArticle($params,$uploadresult){
        $article = new Article();
        $article->article_title = $params['article_title'];
        $article->article_pic = $uploadresult;
        $article->article_desc = $params['article_desc'];
        $article->article_content = $params['article_content'];
        $article->add_time = date('Y-m-d H:i:s');
        $article->article_type = $params['article_type'];
        if(isset($params['extend_type']))
            $article->extend_type = $params['extend_type'];
        return $article->save();
    }

    public function editById($id, $params,$uploadresult){
        if(!$article = Article::findOne($id)) return false;
        $article->article_title = $params['article_title'];
        if($uploadresult)
            $article->article_pic = $uploadresult;
        $article->article_desc = $params['article_desc'];
        $article->article_content = $params['article_content'];
        if(isset($params['extend_type']) && $params['extend_type'])
            $article->extend_type = $params['extend_type'];
        return $article->save();
    }

    public function delete($id){
        if(!$article = Article::findOne($id)) return false;
        $article->is_del = 1;
        return $article->save();
        /*return $article->delete();*/
    }



}